﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebMVC.Helpers;
using WebMVC.Models;

namespace WebMVC
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            ///TODO ADD constraints for parameters
            routes.MapRoute(
                "ImageGenerator",
                "ImageFromText/{fontId}/{color}/{size}",
                new { controller = "StaticData", action = "GetImageFromText" },
                new { fontId = @"\d" }
            );

            routes.MapRoute(
                "PriceCalculatorJSON",
                "Price/{quantity}/{mediaCode}/{methodCode}",
                new { controller = "Product", action = "CalculatePriceJSON" }
            );

            routes.MapRoute(
                "PriceCalculator",
                "Product/CalculatePrice",
                new { controller = "Product", action = "CalculatePrice" },
                new string[] { "WebMVC.Controllers" }
            );

            routes.MapRoute(
                "ProductDefault",
                "Product/List/{type}",
                new { controller = "Product", action = "List", type = "BCARD" },
                new string[] { "WebMVC.Controllers" }
            );

            routes.MapRoute(
                "AboutRoute",
                "About",
                new { controller = "Home", action = "About" }
                );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

            foreach (Route r in routes)
            {
                if (!(r.RouteHandler is SingleLingualMvcRouteHandler))
                {
                    r.RouteHandler = new MultiLingualMvcRouteHandler();
                    r.Url = "{culture}/" + r.Url;
                    if (r.Defaults == null)
                    {
                        r.Defaults = new RouteValueDictionary();
                    }
                    r.Defaults.Add("culture", Culture.ru.ToString());
                    if (r.Constraints == null)
                    {
                        r.Constraints = new RouteValueDictionary();
                    }
                    r.Constraints.Add("culture", new CultureConstraint(Culture.en.ToString(), Culture.ru.ToString(), Culture.hy.ToString()));
                }
            }

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}